package com.sky.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

@Slf4j
@AllArgsConstructor
@Data
public class GaoDeApiUtil {

    private String key;
    private String ShopAddress;


    /**
     * 根据地址获取经纬度
     */
    public String getLatitudeAndLongitude(String address) {
        String URL = "https://restapi.amap.com/v3/geocode/geo?parameters";
        HashMap<String, String> map = new HashMap<>();
        map.put("address", address);
        map.put("key", key);
        String response = HttpClientUtil.doGet(URL, map);
        JSONObject jsonObject = JSON.parseObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
        JSONObject json = jsonArray.getJSONObject(0);
        return json.get("location").toString();
    }

    /**
     * 根据两地经纬度获取直线距离
     */
    public Long getDistance(String origins, String destination) {
        String URL = "https://restapi.amap.com/v3/distance?parameters";
        HashMap<String, String> map = new HashMap<>();
        map.put("key", key);
        origins = ShopAddress;
        map.put("origins", origins);
        map.put("destination", destination);
        map.put("type", "0");
        String response = HttpClientUtil.doGet(URL, map);
        JSONObject jsonObject = JSON.parseObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("results");
        JSONObject json = jsonArray.getJSONObject(0);
        return Long.valueOf(json.get("distance").toString());
    }
}
